package com.homson.servicedomain.message.domain.listener;

import com.homson.coreservice.servicecommon.enumeration.TransTypeEnum;
import com.homson.servicedomain.factory.TransEventProcessFactory;
import com.homson.servicedomain.message.domain.AbstractDomainEventListener;
import com.homson.servicedomain.message.domain.model.TransactionAddedEvent;
import com.homson.servicedomain.processor.AbstractTransEventProcessor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author: homson_leo
 * @contact: hey_超级巨星（Homson_Leo）- 刘泓岑 1196816884@qq.com
 * @time: 2025/10/22 11:38 PM
 **/
@Component
@Slf4j
public class TransactionEventListener extends AbstractDomainEventListener {
    
    @Resource
    private TransEventProcessFactory transEventProcessFactory;
    
    @EventListener(TransactionAddedEvent.class)
    public void TransactionAddedEventHandler(TransactionAddedEvent event){
        TransTypeEnum transType = event.getTransType();
        Optional<AbstractTransEventProcessor> processor = transEventProcessFactory.findBean(transType);
        if(processor.isEmpty()){
            log.error(TransactionEventListener.class.getSimpleName(),"未找到对应的处理器");
            return;
        }
        processor.get().process(event.getAccountId(),event);
        log.info(TransactionEventListener.class.getSimpleName(),String.format("%s-账户%s-金额%s",event.getTransType().name(),event.getAccountId(),event.getAmount()));
    }
}
